"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : autocommit
Case Name   : 关闭自动提交，正常执行事务，开启自动提交，隐式提交事务和自身
Create At   : 2023/01/17
@zou_jialiang0501162244
Description :
    1、兼容B库建表
    2、关闭自动提交，表中插入数据，开启自动提交
    3、新开会话查询表数据
    4、清理环境
Expect      :
    1、成功
    2、执行成功，开启自动提交时提交事务和自身
    3、表中1条数据 insert
    4、执行成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class TestAutocommit(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.tb_name = 't_autocommit_0050'
        self.db_name = 'db_autocommit_0050'
        self.log.info("-----预置兼容B库-----")
        preset_db = f"drop database if exists {self.db_name};" \
                    f"create database {self.db_name} dbcompatibility 'B';"
        preset_db_res = self.pri_sh.execut_db_sql(preset_db)
        self.log.info(preset_db_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, preset_db_res,
                      f"创建兼容B库失败")

    def test_autocommit(self):
        text1 = '-----step1:兼容B库建表并插入数据;expect:成功-----'
        self.log.info(text1)
        preset_data = f"drop table if exists {self.tb_name};" \
                      f"create table {self.tb_name}(c_char char(10));"
        preset_res = self.pri_sh.execut_db_sql(preset_data, '', self.db_name)
        self.log.info(preset_res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, preset_res,
                      f"建表失败: {text1}")

        text2 = '-----step2:关闭自动提交，表中插入数据，开启自动提交，查询autocommit值;' \
                'expect:执行成功，开启自动提交时提交事务和自身，on-----'
        self.log.info(text2)
        sql = f"set autocommit to off;" \
              f"insert into {self.tb_name} values('insert');" \
              f"set autocommit = 1;" \
              f"show autocommit;"
        res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(res)
        self.assertEqual(res.count('SET'), 2, f"执行失败:{text2}")
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, res, f"执行失败:{text2}")
        self.assertEqual(res.splitlines()[-2].strip(), 'on', f"执行失败:{text2}")

        text3 = '-----step3:新开会话查询表数据;expect:1条数据 insert-----'
        self.log.info(text3)
        res = self.pri_sh.execut_db_sql(
            f"select * from {self.tb_name}", dbname=self.db_name)
        self.log.info(res)
        self.assertEqual(res.splitlines()[2].strip(), 'insert',
                         f"执行失败:{text3}")

    def tearDown(self):
        text4 = '-----step4:清理环境 expect:成功-----'
        self.log.info(text4)
        drop_db_res = self.pri_sh.execut_db_sql(
            f"drop database if exists {self.db_name};")
        self.log.info(drop_db_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_db_res,
                      f"执行失败: {text4}")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
